package com.yanqu.road.server.cmd.activity.peakstronghold.cross;

import com.yanqu.road.entity.activity.peakstronghold.UserPeakStrongholdPatronsData;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;
import com.yanqu.road.entity.activity.seasontask.SeasonTaskConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogPeakHoldAttack;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PeakStrongholdWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.peakstronghold.PeakStrongholdModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_PEAK_STRONGHOLD_CHALLENGE_UNION_RESP, desc = "挑战联盟返回")
public class CrossPeakStrongholdChallengeUnionResultCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        PeakStrongHoldConfig config = PeakStrongholdActivityMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return;
        }
        int activityId = config.getActivityInfo().getActivityId();
        PeakStrongholdWarProto.CrossAttackEliminatePatronsRespMsg msg = PeakStrongholdWarProto.CrossAttackEliminatePatronsRespMsg.parseFrom(packet.getBytes());
        ret = msg.getRet();

        PeakStrongholdWarProto.AttackPeakEliminatePatronsRespMsg.Builder resp = PeakStrongholdWarProto.AttackPeakEliminatePatronsRespMsg.newBuilder();
        resp.setRet(ret);

        int patronsId = msg.getChallengePatronsId();
        UserPeakStrongholdPatronsData data = player.getModule(PeakStrongholdModule.class).getChallengePatronsData(activityId, patronsId);
        Property consume = null;
        if (data != null && data.getFreeTimes() >= config.getEliminateFreeTimes()) {
            int buyTimes = data.getBuyTimes();
            consume = config.getEliminateConsume(buyTimes);
            if (consume != null) {
                player.getModule(PreReduceModule.class).restorePreReduce(PropertyHelper.parsePropertyToString(consume));
            }
        }

        if (ret == 0) {
            if (msg.hasPatrons()) {
                resp.setPatrons(msg.getPatrons());
            }
            Property reward = new Property();
            resp.setKill(false);
            if (msg.hasKill()) {
                if (msg.getKill()) {
                    Property killReward = PeakStrongholdActivityMgr.getEliminateKillReward();
                    player.getModule(CurrencyModule.class).addCurrency(killReward, eLogMoneyType.PeakStrongholdActivity, eLogMoneyType.PeakStrongholdActivityChallengeUnion);
                    reward.addProperty(killReward);
                    resp.setKill(msg.getKill());
                }
            }
            if (msg.hasDamage()) {
                String damage = msg.getDamage();
                resp.setDamage(damage);
            }
            if (msg.hasTotalDamage()) {//更新玩家个人总伤害
                player.getModule(PeakStrongholdModule.class).setTotalDamage(new BigInteger(msg.getTotalDamage()));
            }
            if (msg.hasChallengePatronsId()) {
                if (consume != null) {
                    player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.PeakStrongholdActivity, eLogMoneyType.PeakStrongholdActivityChallengeUnion);
                }
                player.getModule(PeakStrongholdModule.class).addBattlePatrons(patronsId);
                data = player.getModule(PeakStrongholdModule.class).getChallengePatronsData(activityId, patronsId);
                Property challengeReward = config.getEliminateChallengeReward(data.getBuyTimes());
                player.getModule(CurrencyModule.class).addCurrency(challengeReward, eLogMoneyType.PeakStrongholdActivity, eLogMoneyType.PeakStrongholdActivityChallengeUnion);
                reward.addProperty(challengeReward);
            }
            if (!reward.isNothing()) {
                resp.setReward(PropertyHelper.parsePropertyToString(reward));
            }
            resp.setUnionInfo(msg.getUnionInfo());

            AutoLogMgr.add(new LogPeakHoldAttack(activityId, config.getRound(), player.getUserInfo().getUnionUid(), player.getUserId(), msg.getChallengePatronsId(),
                    msg.getDamage(), msg.getGroup(), msg.getUnionInfo().getUnionId(),
                    msg.getPatrons().getUserId(), msg.getPatrons().getPatronsId(), msg.getPatrons().getPatronsBlood(), msg.getUnionInfo().getBlood(), resp.getReward()));
        }
        SeasonTaskConditionData conditionData = new SeasonTaskConditionData(BigInteger.valueOf((long)config.getActivityInfo().getType()),BigInteger.valueOf(1L));
        player.notifyListener(eGamePlayerEventType.CrossUnionFightSendPatronTask.getValue(), conditionData);
        player.sendPacket(ClientProtocol.U_PEAK_STRONGHOLD_ELIMINATION_ATTACK, resp);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
